package edu.gatech.cs2340.bytebankers.presenter;

import java.awt.Component;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.List;

import javax.swing.JFileChooser;

import edu.gatech.cs2340.bytebankers.model.Journey;
import edu.gatech.cs2340.bytebankers.model.Leader;
import edu.gatech.cs2340.bytebankers.model.Player;
import edu.gatech.cs2340.bytebankers.model.Wagon;
import edu.gatech.cs2340.bytebankers.model.item.Food;
import edu.gatech.cs2340.bytebankers.notifications.GameEvents;
import edu.gatech.cs2340.bytebankers.notifications.Notifier;
import edu.gatech.cs2340.bytebankers.view.IJourneyOptionView;


public class JourneyOptionPresenter extends Notifier implements
	IJourneyOptionPresenter {

    private IJourneyOptionView journeyOptionView;
    private Journey journey;
    private Leader leader;
    private Wagon wagon;

    private List<Player> list;
    private int currentRation, currentPace;

    public JourneyOptionPresenter(Journey journey) {
	this.journey = journey;
	wagon = journey.getWagon();
	leader = journey.getLeader();
    }


    public void addPresenterListener(IPresenterListener listener) {
    	// TODO Auto-generated method stub
    }


    /**
     * This method will update the view to display the current ration and pace
     */
    public void setView(IJourneyOptionView journeyOptionView) {
    	this.journeyOptionView = journeyOptionView;
    	this.journeyOptionView.setPace(journey.getPace());
    	this.journeyOptionView.setRation(journey.getRationRate());
    	this.journeyOptionView.setBankroll(leader.getMoneyLeft());
    	this.journeyOptionView.setDistanceTravelled(journey.getDistanceTraveled());
    	this.journeyOptionView.setFoodLeft(wagon.getItemQuantityRemaining(journey
			.getItemManager().getItemByType(Food.class)));
    }

    /**
     * This method updates the model
     */
    public void onOkClick() {

	currentRation = journeyOptionView.getRation();
	currentPace = journeyOptionView.getPace();

	journey.setPace(currentPace);
	journey.setRationRate(currentRation);
	this.sendNotification(GameEvents.MENU_CLOSED);

    }


	@Override
	public void onMapClick() {
		sendNotification(GameEvents.MAP_OPENED);
	}


	@Override
	public void onSaveGameClick() {
		// Write to disk with FileOutputStream
		FileOutputStream f_out;
		try {
			//Create a file chooser
			final JFileChooser fc = new JFileChooser();
			
			//In response to a button click:
			
			int returnVal = fc.showSaveDialog(journeyOptionView.getMainPanel());
			
			f_out = new 
				FileOutputStream(fc.getSelectedFile());

				// Write object with ObjectOutputStream
				ObjectOutputStream obj_out = new
					ObjectOutputStream (f_out);
	
				// Write object out to disk
				obj_out.writeObject ( journey );
				
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
	}

    /**
     * used to calculate the ration, but will be doing that in the
     * JourneyPresenter
     * 
     * @param currRation
     * @return
     */
    /*
     * private int calcRation(int currRation){ int count = 0; int result; list =
     * journey.getPlayers(); for (Player p: list){ count++; } result =
     * currRation * count; return result; }
     */
}
